$(document).ready(function() {
	function checkUser() {
		var username    = $.trim($('#username').val());	//用户名
		var name_length = username.length;
		var rgexp       = /^[a-zA-Z0-9_]*$/;
		if(username=='') {
			$('#username_error').attr('class', 'tip-error');
			$('#username_error').html('用户名不能为空!');
		} else if(name_length <6) {
			$('#username_error').attr('class', 'tip-error');
			$('#username_error').html('用户名长度必大于五!');			
		} else if(name_length >=20) {
			$('#username_error').attr('class', 'tip-error');
			$('#username_error').html('用户名长度必小于二十!');			
		} else if(!rgexp.test(username)) {
			$('#username_error').attr('class', 'tip-error');
			$('#username_error').html('用户名必须是数字字母下划线!');
		} else {
			$.post('/user/usernameExists', {'username':username},function(data) {
				if(data == 1) {
					$('#username_error').attr('class', 'tip-error');
					$('#username_error').html('此用户名己经被注册!');
				} else {
					$('#username_error').attr('class', 'tip-tishi');
					$('#username_error').html('6~20个字符，包括字母、数字、下划线');
				}
			});
		}
	}
	
	//检测密码
	function checkPassword() {
		var password = $.trim($("#password").val());
		var pwd_length = password.length;
		if(password == "") {
			$('#password_error').attr('class', 'tip-error');
			$('#password_error').html('密码不能为空!');
		}else if(pwd_length <6) {
			$('#password_error').attr('class', 'tip-error');
			$('#password_error').html('密码长度不能小于六位!');
		} else {
			$('#password_error').attr('class', 'tip-tishi');
			$('#password_error').html('请输入密码!');
		}
	}
	
	//检测重复密码
	function checkRePassword() {
		var password   = $.trim($("#password").val());
		var repassword = $.trim($("#repassword").val());

		if(repassword == '') {
			$('#repassword_error').attr('class', 'tip-error');
			$('#repassword_error').html('重复密码不能为空!');			
		} else if(password == repassword) {
			$('#repassword_error').attr('class', 'tip-tishi');
			$('#repassword_error').html('请再次输入密码!');

		} else {
			$('#repassword_error').attr('class', 'tip-error');
			$('#repassword_error').html('重复密码不正确!');
		}
	}
	//邮箱验证
	function checkEmail() {
		var email = $.trim($("#email").val());
		var rgexp = /^(([a-zA-Z0-9_-])||(\.))+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
		if(rgexp.test(email)) {
			$.post('/user/emailExists', {'email':email},function(data) {
				if(data == 1) {
					$('#email_error').attr('class', 'tip-error');
					$('#email_error').html('此邮箱己经被使用!');
					} else {
						$('#email_error').attr('class', 'tip-tishi');
						$('#email_error').html('请输入常用邮箱，该邮箱用来取回密码!');
					}
			});
		} else {
			$('#email_error').attr('class', 'tip-error');
			$('#email_error').html('请正确填写您的真实邮箱!');
		}
	}
	
	//用户名 失去焦点
	$("#username").blur(function() {
		checkUser();
	});
	//用户名 得到焦点
	$("#username").focus(function() {
		$("#username_tishi").show();
		$("#username_y").hide();
		$("#username_n").hide();
	});
	
	//密码
	$("#password").blur(function() {
		checkPassword();
	});
	//密码 得到焦点
	$("#password").focus(function() {
		$("#password_tishi").show();
		$("#password_y").hide();
		$("#password_n").hide();
	});
	//重复密码
	$("#repassword").blur(function() {
		checkRePassword();
	});
	//重复密码 得到焦点
	$("#repassword").focus(function() {
		$("#repassword_tishi").show();
		$("#repassword_y").hide();
		$("#repassword_n").hide();
	});
	
	//邮箱
	$("#email").blur(function() {
		checkEmail();
	});
	
	$('#register').click(function() {
        checkUser();
		checkPassword();
		checkRePassword();
        checkEmail();
		var username_stat = $('#username_error').attr('class');
		var password_stat = $('#password_error').attr('class');
		var repassword_stat = $('#repassword_error').attr('class');
		var email_stat = $('#email_error').attr('class');
		if(username_stat=='tip-tishi'  && password_stat=='tip-tishi' && repassword_stat=='tip-tishi' && email_stat=='tip-tishi') {
			var username   = $.trim($('#username').val());
			var password   = $.trim($('#password').val());
			var repassword = $.trim($('#repassword').val());
			var email      = $.trim($('#email').val());
			if(username!= '' && password!='' && repassword!='' && email!='' ) {
				$.post('/site/doreg', {'username':username,'password':password,'repassword':repassword,'email':email},function(data) {
					if(data == 0) {
                        ajax_post('/site/login',{'username':username,'password':password},function(){
                            window.location.href='/';
                        });
					} else {
						alert('注册失败');
					}
				});
			} else {
				alert('有没填的哦');
			}
		}
	});

	$('#register').mousedown(function() {
		$(this).attr('class', 'reghover');
	});
	$('#register').mouseup(function() {
		$(this).attr('class', 'reg');
	});
	
});
